<?php
session_start();
class Collection extends Model {
	
    function Collection()
    {
        parent::Model();
    }
	
	function getLastId()
	{
		$sql = "SELECT MAX( ID ) as id FROM COLLECTIONS";
		$query = $this->db->query($sql);
		
		return $query->row()->id;
	}
	
	function getCol($id)
	{
		
		$sql = "SELECT * FROM COLLECTIONS WHERE ID=".$id;
        $query = $this->db->query($sql);
		
		return $query->row();	
	}
	
	function getLast($nb)
    {
		$sql = "SELECT * FROM COLLECTIONS WHERE AUTEUR =".$_SESSION['ID']." ORDER BY ID Desc LIMIT ".$nb;
        $query = $this->db->query($sql);
		
		return $query->result();
    }
	
	function insert($titre, $auteur, $packages, $dir)
	{	
		$IDUS = $_SESSION['ID'];
		$titre		= mysql_real_escape_string($titre);
		$auteur		= $IDUS;
		$packages	= mysql_real_escape_string($packages);
		$date		= time();
		
		$sql = "INSERT INTO COLLECTIONS (ID, TITRE, AUTEUR, DATE, PACKAGES, DIR)
		        VALUES ('',
					'".$titre."',
					'".$auteur."',
					'".$date."',
					'".$packages."',
					'".$dir."'
				)";
		$this->db->query($sql);
		
		

		$ID = mysql_insert_id();
		$IDUS = $_SESSION['ID'];
		$sql2 = "INSERT INTO POSSEDER (ID_US, ID_COL) VALUES('".$IDUS."','".$ID."')";
		//echo $ID;
		//echo $IDUS;
		$this->db->query($sql2);
		return $ID;
	}
	
	function delete($id)
	{
		// suppression du repertoire
		$sql = "SELECT DIR FROM COLLECTIONS WHERE ID=".$id;
		$query = $this->db->query($sql);
		$dir = $query->row()->DIR;
		exec("rm -r ".$this->config->item('dirUpload').$dir);
		
		$sql = "DELETE FROM COLLECTIONS WHERE ID=".$id;
		$this->db->query($sql);
	}
}
?>
